﻿using System;

namespace Simp.CodeModel.CoreFunctions
{
	/// <summary>Encapsulates an integer as an <see cref="IFunction"/>.</summary>
	/// <remarks>This is a primitive function, as it doesn't have sub functions and can't therefore be simplified.</remarks>
	public class IntegerFunction : ParameterLessFunctionBase, INumericFunction, IFunctionSerializer
	{
		private readonly int _value;

		/// <summary>Name of the function.</summary>
		public const string IntegerFunctionName = "Integer";

		/// <summary>Constructor accepting the integer value.</summary>
		/// <param name="value"></param>
		public IntegerFunction(int value)
		{
			_value = value;
		}

		#region INumericFunction Members
		IntegerFunction INumericFunction.GetInteger()
		{
			return this;
		}

		RationalFunction INumericFunction.GetRational()
		{
			return new RationalFunction(_value, 1);
		}

		RealFunction INumericFunction.GetReal(bool force)
		{
			return force ? new RealFunction(_value) : null;
		}
		#endregion

		#region IFunctionSerializer Members
		void IFunctionSerializer.Serialize(AppendOnlyStringBuilder builder, params IFunction[] parameters)
		{
			builder.Append(Value.ToString());
		}
		#endregion

		/// <summary>Integer value encapsulated in this expression.</summary>
		public int Value
		{
			get { return _value; }
		}

		/// <summary>See <see cref="IFunction.FunctionName"/>.</summary>
		public override string FunctionName
		{
			get { return IntegerFunctionName; }
		}

		/// <summary>Accepts the visitor but being a primitive function, it doesn't have sub-functions to visit.</summary>
		/// <param name="visitor"></param>
		/// <returns></returns>
		public override IFunction AcceptVisitor(IFunctionVisitor visitor)
		{
			return null;
		}

		/// <summary>Being a primitive function, it can't be simplified.</summary>
		/// <param name="simplificationMode"/>
		/// <returns></returns>
		protected override IFunction Simplify(SimplificationMode simplificationMode)
		{
			return null;
		}
	}
}